package com.my.domain;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

/**
 * @Author coderMy
 * @Date 2021/3/30
 */
/**
    * 系统用户
    */
@ApiModel(value="com-my-domain-SysUser")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysLoginUser implements UserDetails {
    @ApiModelProperty(value="用户id")
    private Long userId;

    /**
    * 用户名
    */
    @ApiModelProperty(value="用户名")
    private String username;

    /**
    * 密码
    */
    @ApiModelProperty(value="密码")
    private String password;

    /**
     * 权限,自增
     */
    private List<String> auths;
    /**
    * 状态  0：禁用   1：正常
    */
    @ApiModelProperty(value="状态  0：禁用   1：正常")
    private Integer status;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        if (!CollectionUtils.isEmpty(auths)){

            List<SimpleGrantedAuthority> authorities = new ArrayList<>();
            auths.forEach((auth)->{
                SimpleGrantedAuthority authority = new SimpleGrantedAuthority(auth);
                authorities.add(authority);
            });
            return authorities;
        }
        return null;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return String.valueOf(this.username);
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status==1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status==1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status==1;
    }

    @Override
    public boolean isEnabled() {
        return this.status==1;
    }
}